<html>
	<head>
		<script type="text/javascript" src="libs/jquery.js"></script>
		<script type="text/javascript" src="libs/script.js"></script>
		<script>
			localStorage['format']="mp4";
			var myTabs = {};
			$(function () {
				chrome.extension.onRequest.addListener(function(request, sender) {
					if (request.task == "showIcon") {
						myTabs[sender.tab.id]="true";
						chrome.pageAction.setTitle({ tabId: sender.tab.id, title: "Download this video [" + localStorage['format'] + "]"});
						chrome.pageAction.show(sender.tab.id);
					}
				});

				chrome.pageAction.onClicked.addListener(function(tab) {
					var url=tab.url;
					url = url.replace("youtube","3outube");
					url = url.replace("watch",localStorage['format']);
					download(url);
				});
				
				chrome.tabs.onSelectionChanged.addListener(function(tabid, selectInfo) {
					if (myTabs[tabid]) {
						chrome.pageAction.setTitle({ tabId: tabid, title: "Download this video [" + localStorage['format'] + "]"});
						chrome.pageAction.show(sender.tab.id);
					}
				});
				
				chrome.tabs.onRemoved.addListener(function(tabId) {
					delete myTabs[tabId];
				});
			});
			
			function download(url) {
				chrome.tabs.getSelected(null, function(tab){
					chrome.tabs.update(tab.id, { url: url });
					window.close();
  				});
			}
		</script>
	</head>
	<body>
	</body>
</html>